/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	APatrolServiceImpl.java
 * Created：		2016年8月30日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.device.inspect;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.device.inspect.converter.ADeviceInspectConverter;
import com.thor.palmmall.rest.device.inspect.converter.SDeviceInspectConverter;
import com.thor.palmmall.rest.device.inspect.converter.SDeviceInspectQueryParamConverter;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.device.SDeviceInspect;
import com.thor.palmmall.spi.device.inspect.SDeviceInspectService;

/**
 * @author wolv
 *
 */
public class ADeviceInspectServiceImpl implements ADeviceInspectService {

  @Override
  public String create(ADeviceInspect bill, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sDeviceInspectService.create(
        SDeviceInspectConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法创建设备巡检单：" + resp.getMessage());
  }

  @Override
  public String save(ADeviceInspect bill, BeanOperateInfo operateInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");
    SResponse<String> resp = sDeviceInspectService.save(
        SDeviceInspectConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法保存设备巡检单：" + resp.getMessage());
  }

  @Override
  public QueryResult<ADeviceInspect> query(ADeviceInspectQueryParam filter)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(filter, "filter");

    SResponse<SQueryResult<SDeviceInspect>> resp = sDeviceInspectService
        .query(SDeviceInspectQueryParamConverter.getInstance().convert(filter));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), ADeviceInspectConverter.getInstance());
    else
      throw new ThorServiceException("无法查询设备巡检单：" + resp.getMessage());
  }

  @Override
  public ADeviceInspect get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SDeviceInspect> resp = sDeviceInspectService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return ADeviceInspectConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的设备巡检单：" + resp.getMessage());
  }

  @Override
  public String[] queryOutgoingState() throws ThorServiceException {
    SResponse<String[]> resp = sDeviceInspectService.queryOutgoingState();
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("查询设备巡检单流程状态失败：" + resp.getMessage());
    }
  }

  @Autowired
  private SDeviceInspectService sDeviceInspectService;

}
